package controler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class CreateTables {

	/**
	 * @param args
	 */
	
	//Here goes the scripts to create all our tables. Plz keep'em all here
	public static Boolean Create(String path) {

			
			try {
			
				Class.forName("org.apache.derby.jdbc.EmbeddedDriver");

                Connection conn = DriverManager.getConnection("jdbc:derby:"+path+";create=true");

                Statement command = conn.createStatement();
                
                
				// Crates bird table. All items are strings. Although slower it is WAY easier to manage.
				try {
					command.executeUpdate(
							"CREATE TABLE pacotes (id INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), "+
							"preco DECIMAL, "+
							"origem varchar(100), "+
							"destino varchar(100), "+
							"ida INT, "+
							"volta INT, "+
							"classificacaohotel INT, "+
							"nomehotel VARCHAR(100), "+
							"companhiaaereaid INT, "+
							"PRIMARY KEY (id))");
					
					System.out.println("Table pacotes created succesfully");
				}
				catch (SQLException err){
					System.out.println("Can't create table 'pacotes': " + err.getMessage());
				}
					
				try {
					command.executeUpdate(
							"CREATE TABLE dadosusuario (usuario varchar(40), "+
							"senha varchar(20), "+
							"nome varchar(60)," +
							"email varchar(40), "+
							"endereco varchar(100), "+
							"cpf varchar(11), "+
							"rg varchar(12), "+
							"telefone int,"+
							"acesso int, "+
							"PRIMARY KEY (usuario))");				
					System.out.println("Table dadosusuario created succesfully");
				}
				catch (SQLException err){
					System.out.println("Can't create table 'dadosusuario': " + err.getMessage());
				}
				
				
				
				// Crates logs table. All items are strings. Although slower it is WAY easier to manage.
				try {
					command.executeUpdate(
							"CREATE TABLE logs (message varchar(10000), "+
							"data int, "+
							"PRIMARY KEY (message, data))");
					
					System.out.println("Table logs created succesfully");
				}
					catch (SQLException err){
						System.out.println("Can't create table 'logs': " + err.getMessage());
					}
					
				
				//tries to close the db connection
				try {
					command.close();
					conn.close();
				} catch (SQLException e)
				
				{
					System.out.println("Error on close: "+e);
					e.printStackTrace();
				}
				
				}catch(ClassNotFoundException errclass){
	                System.out.println("Missing class! Check your CLASSPATH");
				
				} catch (SQLException e) {
					System.out.println("Error: "+e);
					e.printStackTrace();
				}
			
	

		System.out.println("Tables successfully created.");
		return true;
	}
}